<?php 
/* SVN FILE: $Id: app_model.php 33 2010-01-12 15:15:16Z gigapromoters $ */

/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *								1785 E. Sahara Avenue, Suite 490-204
 *								Las Vegas, Nevada 89104
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package			cake
 * @subpackage		cake.app
 * @since			CakePHP(tm) v 0.2.9
 * @version			$Revision: 33 $
 * @modifiedby		$LastChangedBy: gigapromoters $
 * @lastmodified	$Date: 2010-01-12 20:45:16 +0530 (Tue, 12 Jan 2010) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package		cake
 * @subpackage	cake.app
 */
  require_once(APP.'plugins'.DS.'cms'.DS.'giga_app_model.php');
class AppModel extends GigaAppModel{
	//var $sqlDebug = true;
    
  function initialize() {
      $this->_findMethods['randomRecords'] = true;
      parent::initialize();
  }
  function _findRandomRecords($type,$options,$results = array())
  {
      if($type == "before")
      {
            if (!isset($options['limit'])) {
                $options['limit'] = 10;
            }
            $countOptions = $options;
            $countOptions['limit'] = null;
            $count = $this->find("count", $countOptions);
            $mainQueryOptions = $options;
            if ($count > 10000) {
                $count = 10000;
            }
            $mainQueryOptions['limit'] = $count;
            $mainQueryOptions['fields'] = array("$this->alias.id");
            $mainQueryOptions['callbacks'] = false;
            $ids = $this->find("list", $mainQueryOptions);
            if(!empty($ids))
            {
            	$ids = array_rand($ids, $options['limit']);
            }
            $options["conditions"] = array("$this->alias.id" => $ids);
      }else
      {
          return $results;
      }
      return $options;
  }
  function _uniqueCacheKey($id, $field)
  {
    $cacheKey = cr('appCode').'_'.low($this->alias).'_'.$field.'_'.$id;
    return $cacheKey;
  }
}
App::import('Vendor','SiteUrl');
?>